<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="74px">
      <el-form-item label="供应商名称" prop="supplierName" label-width="85px">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入供应商名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
          style="width: 150px"
        />
      </el-form-item>
      <el-form-item label="供应商分类" prop="type" label-width="85px">
        <el-select v-model="queryParams.type" placeholder="请选择供应商分类"  clearable size="small">
          <el-option v-for="(item, index) in typeOptions" :key="index" :label="item.label"
                     :value="item.value" :disabled="item.disabled"  style="width: 150px"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="供应商区域" prop="region" label-width="85px">
        <el-input
          v-model="queryParams.region"
          placeholder="请输入供应商区域"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
          style="width: 150px"
        />
      </el-form-item>
      <el-form-item label="供应商状态" prop="status" label-width="85px">
        <el-select v-model="queryParams.status" placeholder="请选择供应商状态" clearable size="small">
          <el-option v-for="(item, index) in statusOptions" :key="index" :label="item.label"
                     :value="item.value" :disabled="item.disabled"  style="width: 150px"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" style="margin-left: 25px">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:supplier:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:supplier:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:supplier:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:supplier:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="supplierList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="供应商名称" align="center" prop="supplierName" />
      <el-table-column label="供应商编号" align="center" prop="code" />
      <el-table-column label="供应商分类" align="center" prop="type" />
      <el-table-column label="供应商区域" align="center" prop="region" />
      <el-table-column label="联系人姓名" align="center" prop="relName" />
      <el-table-column label="联系方式" align="center" prop="userPhone" />
      <el-table-column label="供应商状态" align="center" prop="status" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:supplier:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:supplier:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="75px" style="font-family:楷体;margin-top: -10px">
        <div style="height:30px;border-bottom:1px solid #f2f2f2;width: 900px;margin: 0 5px 10px"><p style="font-size: 18px;margin-left: 5px;font-weight: bold;border-left: #006eff 5px solid;padding-left: 10px">基础信息</p></div>
        <div style="width: 900px;margin: 0 auto;margin-left:10px;display: inline-block">
          <el-form-item label="供应商名称" prop="supplierName" label-width="110px" style="float: left">
            <el-input v-model="form.supplierName" placeholder="请输入供应商名称" style="width: 170px" />
          </el-form-item>
          <el-form-item label="供应商编号" prop="code" label-width="110px" style="float: left">
            <el-input v-model="form.code" placeholder="请输入供应商编号" style="width: 170px" />
          </el-form-item>
          <el-form-item label="供应商分类" prop="type" label-width="110px" style="float: left">
            <el-select v-model="form.type" placeholder="请选择供应商分类">
              <el-option v-for="(item, index) in typeOptions" :key="index" :label="item.label"
                         :value="item.value" :disabled="item.disabled" style="width: 170px"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="供应商区域" prop="region" label-width="110px" style="float: left">
            <el-input v-model="form.region" placeholder="请输入供应商区域" style="width: 450px" />
          </el-form-item>
          <el-form-item label="供应商备注" prop="remark" label-width="110px" style="float: left">
            <el-input v-model="form.remark" placeholder="请输入供应商备注"  style="width: 210px"/>
          </el-form-item>
          <el-form-item label="供应商详细地址" prop="adress" label-width="110px" style="float: left">
            <el-input v-model="form.adress" placeholder="请输入供应商详细地址" style="width: 450px" />
          </el-form-item>
          <el-form-item label="供应商状态" label-width="110px" style="float: left">
            <el-switch v-model="form.status"></el-switch>
          </el-form-item>
        </div>
        <div style="height:30px;border-bottom:1px solid #f2f2f2;width: 900px;margin: 0 5px 10px"><p style="font-size: 18px;margin-left: 5px;font-weight: bold;border-left: #006eff 5px solid;padding-left: 10px">联系人信息</p></div>
        <div style="width: 900px;margin: 0 auto;margin-left:10px;display: inline-block">
          <el-form-item label="联系人姓名" prop="relName" label-width="110px" style="float: left">
            <el-input v-model="form.relName" placeholder="请输入联系人姓名" style="width: 170px" />
          </el-form-item>
          <el-form-item label="联系方式" prop="userPhone" label-width="110px" style="float: left">
            <el-input v-model="form.userPhone" placeholder="请输入联系方式" style="width: 170px" />
          </el-form-item>
          <el-form-item label="联系人职位" prop="position" label-width="110px" style="float: left">
            <el-input v-model="form.position" placeholder="请输入联系人职位" style="width: 170px" />
          </el-form-item>
          <el-form-item label="电子邮件" prop="email" label-width="110px" style="float: left">
            <el-input v-model="form.email" placeholder="请输入电子邮件" style="width: 170px" />
          </el-form-item>
          <el-form-item label="邮编" prop="zipNo" label-width="110px" style="float: left">
            <el-input v-model="form.zipNo" placeholder="请输入邮编" style="width: 170px" />
          </el-form-item>
        </div>
        <div style="height:30px;border-bottom:1px solid #f2f2f2;width: 900px;margin: 0 5px 10px"><p style="font-size: 18px;margin-left: 5px;font-weight: bold;border-left: #006eff 5px solid;padding-left: 10px">财务信息</p></div>
        <div style="width: 900px;margin: 0 auto;margin-left:10px;display: inline-block">
          <el-form-item label="更新者" prop="updateUser" label-width="110px" style="float: left">
            <el-input v-model="form.updateUser" placeholder="请输入更新者" style="width: 170px" />
          </el-form-item>
          <el-form-item label="创建者" prop="createUser" label-width="110px" style="float: left">
            <el-input v-model="form.createUser" placeholder="请输入创建者" style="width: 170px" />
          </el-form-item>
          <el-form-item label="发票排头" prop="invoiceHeader" label-width="110px" style="float: left">
            <el-input v-model="form.invoiceHeader" placeholder="请输入发票排头" style="width: 170px" />
          </el-form-item>
          <el-form-item label="纳税人识别号" prop="taxpayerId" label-width="110px" style="float: left">
            <el-input v-model="form.taxpayerId" placeholder="请输入纳税人识别号" style="width: 170px" />
          </el-form-item>
          <el-form-item label="开户名称" prop="accountName" label-width="110px" style="float: left">
            <el-input v-model="form.accountName" placeholder="请输入开户名称" style="width: 170px" />
          </el-form-item>
          <el-form-item label="银行账号" prop="bankAccount" label-width="110px" style="float: left">
            <el-input v-model="form.bankAccount" placeholder="请输入银行账号" style="width: 170px" />
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSupplier, getSupplier, delSupplier, addSupplier, updateSupplier, exportSupplier } from "@/api/system/supplier";

export default {
  name: "Supplier",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商信息表格数据
      supplierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierName: null,
        code: null,
        type: null,
        region: null,
        adress: null,
        relName: null,
        userPhone: null,
        status: null,
        position: null,
        updateUser: null,
        createUser: null,
        email: null,
        zipNo: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        supplierName:[
          {required: true, message: "供应商名称不能为空", trigger: "blur" }
        ],
        type:[
          {required: true, message: "供应商分类不能为空", trigger: "blur" },
          {required:true,message:"请选择供应商分类",trigger:'change'}
        ],
        relName:[{required: true, message: "联系人姓名不能为空", trigger: "blur"}]
        ,
        userPhone:[{required: true, message: "联系方式不能为空", trigger: "blur"}]
        ,
        mobile: [{pattern: /^1(3|4|5|7|8|9)\d{9}$/, message: '手机号格式错误', trigger: 'blur'}]
      },
      typeOptions:[{
        "label":"分类一",
        "value":1
      },
        {
          "label":"分类二",
          "value":2
        },
        {
          "label":"分类三",
          "value":3
        }],
      statusOptions:[{
        "label":"true",
        "value":true
      },
        {
          "label":"false",
          "value":false
        }]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询供应商信息列表 */
    getList() {
      this.loading = true;
      listSupplier(this.queryParams).then(response => {
        this.supplierList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        supplierId: null,
        supplierName: null,
        code: null,
        type: null,
        region: null,
        adress: null,
        remark: null,
        relName: null,
        userPhone: null,
        status: "0",
        position: null,
        createTime: null,
        updateUser: null,
        updateTime: null,
        createUser: null,
        email: null,
        zipNo: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.supplierId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加供应商信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const supplierId = row.supplierId || this.ids
      getSupplier(supplierId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供应商信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.supplierId != null) {
            updateSupplier(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSupplier(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const supplierIds = row.supplierId || this.ids;
      this.$modal.confirm('是否确认删除供应商信息编号为"' + supplierIds + '"的数据项？').then(function() {
        return delSupplier(supplierIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有供应商信息数据项？').then(() => {
        this.exportLoading = true;
        return exportSupplier(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
